var merchantNo;
$(function () {
	
	var thisURL = document.URL;   
	var getval =thisURL.split('?')[1];  
	merchantNo= getval.split("=")[1];
	$('#merchantNo').val(merchantNo);
    $("#jqGrid").jqGrid({
        url: '../bankInfo/list',
        datatype: "json",
        postData:{
        	merchantNo:merchantNo
        	},
        colModel: [
            { label: '主键', name: 'id', index: 'id', width: 100,hidden:true},    
            { label: '开户人名称', name: 'bankUser', index: 'bank_user', width: 100 }, 
			{ label: '商户号', name: 'merchantNo', index: 'merchant_no', hidden:true }, 
			{ label: '开户行名称', name: 'bankName', index: 'bank_name', width: 150 }, 
			{ label: '开户行卡号', name: 'bankCard', index: 'bank_card', width: 150 }, 			
			{ label: '联系方式', name: 'mobile', index: 'mobile', width: 100 },
			{ label: '用途说明', name: 'useDesc', index: 'useDesc', width: 100 }, 
			{ label: '状态', name: 'status',width:60 ,hidden:false},
			{ label: '状态', name: 'payState',width:60, index: 'pay_state' ,
				formatter: function(value, options, row){
					if(value == 1){
						return '<span class="label label-success" >启用</span>';
						//return '<a onclick="editState(' + JSON.stringify(row.merchantNo).replace(/"/g, '&quot;') + ',2,'+row.id+')" target="view_window"><span class="label label-success" >启用</span></a>';
					}else if(value == 2){
						return '<span class="label label-success" >禁用</span>';
						//return '<a onclick="editState(' + JSON.stringify(row.merchantNo).replace(/"/g, '&quot;') + ',1,'+row.id+')" target="view_window"><span class="label label-success" >禁用</span></a>';
					}else {
						return '';
					}
				}	
			}
			/*{ label: '支付渠道商', name: 'payChannelId', width: 100 }, */
        ],
		viewrecords: true,
        height: "100%",
        rowNum: 10,
		rowList : [10,20,30],
        rownumbers: false, 
        rownumWidth: 5, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            id: "id", 
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});


function editState(merchantNo,state,id){
	var bankInfo = {
			merchantNo : merchantNo,
			id:id,
			payState:state
	};
	//console.log(bankInfo);
	confirm('确定执行该操作？', function(){
		$.ajax({
			type: "POST",
		    url: "../bankInfo/update",
		    data: JSON.stringify(
		    		bankInfo
		    ),
		    success: function(r){
		    	if(r.code == 0){
					alert(r.msg, function(index){
						vm.reload();
					});
				}else{
					alert(r.msg);
				}
			}
		});
	});
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			payChannelId: null
		},
		showList: true,
		title: null,
		bankInfo: {},
		payChannel: {},
		selected: '',
		payTypeOptions: [
	      { text: '微信', value: '1' },
	      { text: '支付宝', value: '2' }
	    ],
	    iscreditCardoptions:[{text:"否",value:"0"},
	    	{text:"是",value:"1"}],
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(type){
			
			vm.showList = false;
			vm.title = "新增";
			if(type == 1){
				$("#bank").show();
				$("#channel").hide();
				vm.bankInfo = {
					merchantNo : merchantNo
				};
			}else{
				var id = getSelectedRow();
				if(id == '' || id == null){
					vm.reload();
				}
				rowData = $('#jqGrid').jqGrid('getRowData',id);
				var bankId = rowData.id;
				var bankName = rowData.bankName;
				var bankCard = rowData.bankCard;
				vm.payChannel={
						bankId : bankId,
						bankName : bankName,
						merchantNo : merchantNo
				};
				vm.bankInfo={
						bankCard : bankCard,
						bankName : bankName,
				};
				$("#channel").show();
				$("#bank").hide();
				/*var rowData = $('#jqGrid').jqGrid('getRowData',id);
				var payChannelId = rowData.payChannelId;
				if(payChannelId == '' || payChannelId == null){
					$("#channel").show();
					$("#bank").hide();
					vm.payChannel={
							bankId : bankId,
							bankName : bankName,
							merchantNo : merchantNo
					};
				}else{
					alert('该银行信息已关联支付渠道');
					vm.reload();
				}*/
				
			}
			
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			$("#bank").show();
			$("#channel").hide();
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
		},
		editState: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			rowData = $('#jqGrid').jqGrid('getRowData',id);
			var merchantNo = rowData.merchantNo;
			var payState = rowData.status;
			if(rowData.status == '1'){
				payState = 2;
			}else if(rowData.status == '2'){
				payState = 1;
			}
			var bankInfo = {
					merchantNo : merchantNo,
					id:id,
					payState : payState
			};
			//console.log(bankInfo);
			confirm('确定执行该操作？', function(){
				$.ajax({
					type: "POST",
				    url: "../bankInfo/update",
				    data: JSON.stringify(
				    		bankInfo
				    ),
				    success: function(r){
				    	if(r.code == 0){
							alert(r.msg, function(index){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		addBatchCode: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			rowData = $('#jqGrid').jqGrid('getRowData',id);
			var status = rowData.status;
			if(status == '1'){
				var merchantId = rowData.merchantNo;
				var bankId = rowData.id;
				var qrCode = {
						merchantId : merchantId,
						bankId:bankId
				};
				confirm('确定执行该操作？', function(){
					$.ajax({
						type: "POST",
					    url: "../qrCode/addBatchCode",
					    data: JSON.stringify(qrCode),
					    success: function(r){
					    	if(r.code == 0){
								alert("操作成功", function(index){
									vm.reload();
								});
							}else{
								alert("数据异常，请重新操作");
							}
						}
					});
				});
			}else if(status == '2'){
				alert('请选择状态启用的银行卡进行此操作');
				return ;
			}
		},
		addChannel: function(){
			vm.showList = false;
			vm.title = "新增渠道商";
			vm.payChannel = {
			};
		},
		saveOrUpdate: function (event) {
			var url = vm.bankInfo.id == null ? "../bankInfo/save" : "../bankInfo/update";
			$("#submitBtn2").attr("disabled","disabled");
			if(vm.bankInfo.bankUser == null || "" == vm.bankInfo.bankUser){
				alert('开户人名称不能为空');
				return;
			}
			if(vm.bankInfo.bankName == null || "" == vm.bankInfo.bankName){
				alert('开户行名称不能为空');
				return;
			}
			var bankCard = vm.bankInfo.bankCard;
			if(bankCard == null || "" == bankCard){
				alert('开户行卡号不能为空');
				return;
			}else if(!(/^[0-9]*$/.test(bankCard))){
				alert('开户行卡号只能是数字');
				return;
			}
			
			var mobile = vm.bankInfo.mobile;
			if(mobile == null || "" == mobile){
				alert('联系电话不能为空');
				return;
			}else if (!(/^((\d{3}-\d{8}|\d{4}-\d{7,8})|(1[3|5|7|8][0-9]{9}))$/.test(mobile))) {
	             alert("联系电话填写的格式不对，请正确填写");
	             return ;
			}
			
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.bankInfo),
			    success: function(r){
			    	if(r.code == 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
			    	$("#submitBtn2").removeAttr("disabled");
				}
			});
		},
		saveChannel: function (event) {
			var url = "../payChannel/save";
			$("#submitBtn").attr("disabled","disabled");
			if(vm.payChannel.payType == null || "" == vm.payChannel.payType){
				alert('请选择一个支付渠道类型');
				return;
			}
			
			if(vm.payChannel.payChannelNo == null || "" == vm.payChannel.payType){
				alert('支付渠道编号不能为空');
				return;
			}
			if(vm.payChannel.payType == null || "" == vm.payChannel.payType){
				alert('支付渠道类型不能为空');
				return;
			}
			if(vm.payChannel.payKey == null || "" == vm.payChannel.payType){
				alert('支付渠道密钥不能为空');
				return;
			}
			var payRate = vm.payChannel.payRate;
			//有两位小数的正实数：^[0-9]+(.[0-9]{2})?$
			if(payRate == null  || "" == payRate){
				alert('支付渠道费率不能为空');
				return;
			}else if(!(/^[0-9]+(.[0-9]{2})?$/.test(payRate))) {
	             alert("费率只能填写两位小数的正实数");
	             return ;
			}
			var maxTransactionMoney = vm.payChannel.maxTransactionMoney;
			//非零的正整数：^[1-9]\d*$
			if(maxTransactionMoney == null || "" == maxTransactionMoney){
				alert('支付渠道最大限额不能为空');
				return;
			}else if (!(/^[1-9]\d*$/.test(maxTransactionMoney))) {
	             alert("最大限额只能填写非零的正整数");
	             return ;
			}
			var dayMaxMoney = vm.payChannel.dayMaxMoney;
			if(dayMaxMoney == null || "" == dayMaxMoney){
				alert('日累计限额不能为空');
				return;
			}else if (!(/^[1-9]\d*$/.test(dayMaxMoney))) {
	             alert("日累计限额只能填写非零的正整数");
	             return ;
			}
			if(vm.payChannel.iscreditCard == null || "" ==  vm.payChannel.payType){
				alert('是否支持信用卡不能为空');
				return;
			}
			
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.payChannel),
			    success: function(r){
			    	if(r.code == 0){
						alert(r.msg, function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
			    	$("#submitBtn").removeAttr("disabled");
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../bankInfo/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			//console.log(id)
			$.get("../bankInfo/info/"+id, function(r){
                vm.bankInfo = r.bankInfo;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			page = 1;
			var keyword = $("#keyword").val();
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{keyword:keyword},
            }).trigger("reloadGrid");
		},
	}
});